package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.InfoCollect;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.InfoCollectRepository;
import co.yixiang.modules.shop.service.InfoCollectService;
import co.yixiang.modules.shop.service.dto.InfoCollectDto;
import co.yixiang.modules.shop.service.dto.InfoCollectQueryCriteria;
import co.yixiang.modules.shop.service.mapper.InfoCollectMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "infoCollect")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class InfoCollectServiceImpl implements InfoCollectService {

    private final InfoCollectRepository infoCollectRepository;

    private final InfoCollectMapper infoCollectMapper;

    public InfoCollectServiceImpl(InfoCollectRepository infoCollectRepository, InfoCollectMapper infoCollectMapper) {
        this.infoCollectRepository = infoCollectRepository;
        this.infoCollectMapper = infoCollectMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(InfoCollectQueryCriteria criteria, Pageable pageable){
        Page<InfoCollect> page = infoCollectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(infoCollectMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<InfoCollectDto> queryAll(InfoCollectQueryCriteria criteria){
        return infoCollectMapper.toDto(infoCollectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public InfoCollectDto findById(Integer id) {
        InfoCollect infoCollect = infoCollectRepository.findById(id).orElseGet(InfoCollect::new);
        ValidationUtil.isNull(infoCollect.getId(),"InfoCollect","id",id);
        return infoCollectMapper.toDto(infoCollect);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public InfoCollectDto create(InfoCollect resources) {
        return infoCollectMapper.toDto(infoCollectRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(InfoCollect resources) {
        InfoCollect infoCollect = infoCollectRepository.findById(resources.getId()).orElseGet(InfoCollect::new);
        ValidationUtil.isNull( infoCollect.getId(),"InfoCollect","id",resources.getId());
        infoCollect.copy(resources);
        infoCollectRepository.save(infoCollect);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            infoCollectRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InfoCollectDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InfoCollectDto infoCollect : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", infoCollect.getUserId());
            map.put("资讯标题", infoCollect.getTitle());
            map.put("资讯类型", infoCollect.getType());
            map.put("资讯链接", infoCollect.getUrl());
            map.put("添加时间", infoCollect.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}